import tushare as ts
import pandas as pd
from ted_funs import *

# token = 'b2466a123789dae8056ddd5f58efabf0f93e6648e4f886ad110b4fc2'
token = '8d09dbe014b31fc27544116b676ba4ff894d0b3b49105e1b8be3515a'
pro = ts.pro_api(token)


def main():
	print('...start...')
	datas = pro.stock_basic(exchange='', list_status='L', fields='ts_code')

	lst = []

	count = len(datas)
	for data in datas['ts_code']:
		print(count)
		# print(data)
		query = 'select ts_code, trade_date, close from daily where ts_code = \'{}\''.format(data)
		df_new = mysql_read(query)
		# df = pd.read_sql_query(con=engine_ts.connect(), sql=text(query))

		# df_new = df_new.sort_values(by='trade_date', ascending=True)
		df_new['MA'] = df_new['close'].rolling(5).mean()

		df_new['flag'] = df_new['close'] - df_new['MA']
		# print(df_new)
		# break
		df_new = df_new.iloc[-10:]
		result = (df_new['flag'] > 0).sum()

		if result == 10:
			print(data)
			lst.append(df_new['ts_code'][:1])

		count -= 1
		# break

	if len(lst):
		print(lst)
		last = pd.concat(lst, ignore_index=True)
		last.to_excel('gp'+'.xlsx')
		# print('xxx')

	engine_ts.dispose()
	print("...Done...")


if __name__ == "__main__":
	main()


